#include "motorform.h"
#include "ui_motorform.h"
#include <QDebug>
#include <QSettings>
#include <QTextCodec>
#include <QGraphicsDropShadowEffect>

MotorForm::MotorForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MotorForm)
{
    ui->setupUi(this);
    for (int i = 1; i <= 11; ++i) {
        QString comboName = QString("cBox_1_%1").arg(i);
        QComboBox *combo = this->findChild<QComboBox*>(comboName);

        if (!combo) {
            qWarning() << "Combobox not found:" << comboName;
            continue;
        }
        combo->clear();
        QString prefix = (i % 2 == 1) ? "DI " : "DO ";
        for (int j = 0; j <= 15; ++j) {
            combo->addItem(prefix + QString::number(j));
        }
    }
    for (int j = 0; j <= 15; ++j) {
        ui->cBox_1_12->addItem("DI " + QString::number(j));
    }

    RdConfig();

    m_rtn = controller.initialize(); // 初始化控制卡和定时器
    if(m_rtn == 0){
       on_Bt_1_1_clicked();
       on_Bt_1_2_clicked();
       on_Bt_1_3_clicked();
    }

    //内层窗口添加对应的阴影效果
    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectC = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectD = new QGraphicsDropShadowEffect(this);

    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);

    shadow_effectC->setOffset(0, 0);
    shadow_effectC->setColor(QColor(150,150,150));
    shadow_effectC->setBlurRadius(6);

    shadow_effectD->setOffset(0, 0);
    shadow_effectD->setColor(QColor(150,150,150));
    shadow_effectD->setBlurRadius(6);


    ui->motor_gBox_1->setGraphicsEffect(shadow_effectA);
    // ui->main_gBox_2->setGraphicsEffect(shadow_effectB);
    // ui->main_gBox_3->setGraphicsEffect(shadow_effectC);
    // ui->main_gBox_4->setGraphicsEffect(shadow_effectD);

}

MotorForm::~MotorForm()
{
    delete ui;
}


void MotorForm::on_Bt_motor_strart_clicked()
{
    // 启动按钮
    controller.startMotorButton(m_motorInfo.DI_MotorStart, m_motorInfo.DO_MotorStart);
}


void MotorForm::on_Bt_motor_stop_clicked()
{
    // 停止按钮
    controller.startStopButton(m_motorInfo.DI_MotorStop, m_motorInfo.DO_MotorStart);
}


void MotorForm::on_Bt_motor_emo_clicked()
{
    // // 急停按钮
    // controller.startEmergencyButton(m_motorInfo.DI_MotorEMOStop, m_motorInfo.DO_MotorStart);
    // controller.emergencyStop();

    controller.closeAllDO();
}


void MotorForm::on_Bt_close_all_clicked()
{
    controller.closeAllDO();
}

// sensor1
void MotorForm::on_Bt_1_1_clicked()
{
    WdConfig(1);

    // 传感器1: 上升沿触发
    controller.startSensor1(m_motorInfo.DI_Sensor1, m_motorInfo.DO_Sensor1); // DI0, DO1
}

// sensor2
void MotorForm::on_Bt_1_2_clicked()
{
    WdConfig(2);

    // 传感器2: 下降沿触发
    controller.startSensor2(m_motorInfo.DI_Sensor2, m_motorInfo.DO_Sensor2); // DI2, DO3
}

// sensor3
void MotorForm::on_Bt_1_3_clicked()
{
    WdConfig(3);

    // 传感器3: 编码器触发
    controller.startSensor3(m_motorInfo.DI_Sensor3, m_motorInfo.DO_Sensor3, m_motorInfo.Start_Count_Sensor3, m_motorInfo.End_Count_Sensor3); // DI4, DO5, 开始计数100, 结束计数200
}


void MotorForm::soltInMain(int sig)
{
    if(sig){
        on_Bt_motor_strart_clicked();
    }else{
        on_Bt_motor_stop_clicked();
    }
}

void MotorForm::RdConfig()
{
    QString Path = "configs/motor.ini";
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

    m_motorInfo.DI_Sensor1 = iniRead.value("MOTOR_CONTROL_INFO/DI_Sensor1").toInt();
    m_motorInfo.DO_Sensor1 = iniRead.value("MOTOR_CONTROL_INFO/DO_Sensor1").toInt();
    m_motorInfo.DI_Sensor2 = iniRead.value("MOTOR_CONTROL_INFO/DI_Sensor2").toInt();
    m_motorInfo.DO_Sensor2 = iniRead.value("MOTOR_CONTROL_INFO/DO_Sensor2").toInt();
    m_motorInfo.DI_Sensor3 = iniRead.value("MOTOR_CONTROL_INFO/DI_Sensor3").toInt();
    m_motorInfo.DO_Sensor3 = iniRead.value("MOTOR_CONTROL_INFO/DO_Sensor3").toInt();
    m_motorInfo.Start_Count_Sensor3 = iniRead.value("MOTOR_CONTROL_INFO/Start_Count_Sensor3").toInt();
    m_motorInfo.End_Count_Sensor3 = iniRead.value("MOTOR_CONTROL_INFO/End_Count_Sensor3").toInt();
    m_motorInfo.DI_Sensor4 = iniRead.value("MOTOR_CONTROL_INFO/DI_Sensor4").toInt();
    m_motorInfo.DO_Sensor4 = iniRead.value("MOTOR_CONTROL_INFO/DO_Sensor4").toInt();
    m_motorInfo.Start_Count_Sensor4 = iniRead.value("MOTOR_CONTROL_INFO/Start_Count_Sensor4").toInt();
    m_motorInfo.End_Count_Sensor4 = iniRead.value("MOTOR_CONTROL_INFO/End_Count_Sensor4").toInt();
    m_motorInfo.DI_MotorStart = iniRead.value("MOTOR_CONTROL_INFO/DI_MotorStart").toInt();
    m_motorInfo.DO_MotorStart = iniRead.value("MOTOR_CONTROL_INFO/DO_MotorStart").toInt();
    m_motorInfo.DI_MotorStop = iniRead.value("MOTOR_CONTROL_INFO/DI_MotorStop").toInt();
    m_motorInfo.DI_MotorEMOStop = iniRead.value("MOTOR_CONTROL_INFO/DI_MotorEMOStop").toInt();

    ui->cBox_1_1->setCurrentIndex(m_motorInfo.DI_Sensor1);
    ui->cBox_1_2->setCurrentIndex(m_motorInfo.DO_Sensor1);
    ui->cBox_1_3->setCurrentIndex(m_motorInfo.DI_Sensor2);
    ui->cBox_1_4->setCurrentIndex(m_motorInfo.DO_Sensor2);
    ui->cBox_1_5->setCurrentIndex(m_motorInfo.DI_Sensor3);
    ui->cBox_1_6->setCurrentIndex(m_motorInfo.DO_Sensor3);
    ui->lineEdit_5->setText(QString::number(m_motorInfo.Start_Count_Sensor3));
    ui->lineEdit_6->setText(QString::number(m_motorInfo.End_Count_Sensor3));

    ui->cBox_1_7->setCurrentIndex(m_motorInfo.DI_Sensor4);
    ui->cBox_1_8->setCurrentIndex(m_motorInfo.DO_Sensor4);
    ui->lineEdit_7->setText(QString::number(m_motorInfo.Start_Count_Sensor4));
    ui->lineEdit_8->setText(QString::number(m_motorInfo.End_Count_Sensor4));

    ui->cBox_1_9->setCurrentIndex(m_motorInfo.DI_MotorStart);
    ui->cBox_1_10->setCurrentIndex(m_motorInfo.DO_MotorStart);
    ui->cBox_1_11->setCurrentIndex(m_motorInfo.DI_MotorStop);
    ui->cBox_1_12->setCurrentIndex(m_motorInfo.DI_MotorEMOStop);

}

void MotorForm::WdConfig(int index)
{
    QString Path = "configs/motor.ini";
    QSettings iniWrite(Path, QSettings::IniFormat);
    iniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));


    switch (index) {
    case 1:
        m_motorInfo.DI_Sensor1 = ui->cBox_1_1->currentIndex();
        m_motorInfo.DO_Sensor1 = ui->cBox_1_2->currentIndex();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_Sensor1"), m_motorInfo.DI_Sensor1);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DO_Sensor1"), m_motorInfo.DO_Sensor1);
        break;
    case 2:
        m_motorInfo.DI_Sensor2 = ui->cBox_1_3->currentIndex();
        m_motorInfo.DO_Sensor2 = ui->cBox_1_4->currentIndex();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_Sensor2"), m_motorInfo.DI_Sensor2);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DO_Sensor2"), m_motorInfo.DO_Sensor2);
        break;
    case 3:
        m_motorInfo.DI_Sensor3 = ui->cBox_1_5->currentIndex();
        m_motorInfo.DO_Sensor3 = ui->cBox_1_6->currentIndex();
        m_motorInfo.Start_Count_Sensor3 = ui->lineEdit_5->text().toInt();
        m_motorInfo.End_Count_Sensor3 = ui->lineEdit_6->text().toInt();

        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_Sensor3"), m_motorInfo.DI_Sensor3);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DO_Sensor3"), m_motorInfo.DO_Sensor3);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/Start_Count_Sensor3"), m_motorInfo.Start_Count_Sensor3);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/End_Count_Sensor3"), m_motorInfo.End_Count_Sensor3);
        break;
    case 4:
        m_motorInfo.DI_Sensor4 = ui->cBox_1_7->currentIndex();
        m_motorInfo.DO_Sensor4 = ui->cBox_1_8->currentIndex();
        m_motorInfo.Start_Count_Sensor4 = ui->lineEdit_7->text().toInt();
        m_motorInfo.End_Count_Sensor4 = ui->lineEdit_8->text().toInt();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_Sensor4"), m_motorInfo.DI_Sensor4);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DO_Sensor4"), m_motorInfo.DO_Sensor4);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/Start_Count_Sensor4"), m_motorInfo.Start_Count_Sensor4);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/End_Count_Sensor4"), m_motorInfo.End_Count_Sensor4);
        break;
    case 5:
        m_motorInfo.DI_MotorStart = ui->cBox_1_9->currentIndex();
        m_motorInfo.DO_MotorStart = ui->cBox_1_10->currentIndex();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_MotorStart"), m_motorInfo.DI_MotorStart);
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DO_MotorStart"), m_motorInfo.DO_MotorStart);
        break;
    case 6:
        m_motorInfo.DI_MotorStop = ui->cBox_1_11->currentIndex();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_MotorStop"), m_motorInfo.DI_MotorStop);
        break;
    case 7:
        m_motorInfo.DI_MotorEMOStop = ui->cBox_1_12->currentIndex();
        iniWrite.setValue(QString("MOTOR_CONTROL_INFO/DI_MotorEMOStop"), m_motorInfo.DI_MotorEMOStop);
        break;
    }
}



